package edu.hawaii.solardecathlon.page.settings;

import java.util.Map;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import edu.hawaii.solardecathlon.page.BasePanel;

/**
 * The User Settings Page.
 * 
 * @author Michael Cera
 * @author Bret Ikehara
 */
public class SettingsUserSettingsPanel extends BasePanel {

  /**
   * Support serialization.
   */
  private static final long serialVersionUID = 1L;

  /**
   * Constructor for the User Settings Panel.
   * 
   * @param id String
   */
  public SettingsUserSettingsPanel(String id) {
    super(id);
  }

  /**
   * Adds all components to the User Settings panel.
   */
  @Override
  protected void onBeforeRender() {
    super.onBeforeRender();

    Map<String, String> sessionProps = getSessionProperties();

    final FeedbackPanel feedback = new FeedbackPanel("UserSettingsFeedback");
    feedback.setOutputMarkupId(true);

    TextField<String> tfUserEmail =
        new TextField<String>("UserSettingsEmail", new PropertyModel<String>(sessionProps,
            "user_email"));
    tfUserEmail.setRequired(true);
    tfUserEmail.setType(String.class);
    tfUserEmail.add(new AttributeModifier("value",
        new Model<String>(sessionProps.get("user_email"))));

    TextField<String> tfUserPhone =
        new TextField<String>("UserSettingsPhone", new PropertyModel<String>(sessionProps,
            "user_phone"));
    tfUserPhone.setRequired(true);
    tfUserPhone.setType(String.class);
    tfUserPhone.add(new AttributeModifier("value",
        new Model<String>(sessionProps.get("user_phone"))));

    Form<String> form = new Form<String>("UserSettingsForm");
    form.add(new Label("UserSettingsDisplayName", sessionProps.get("user_name")));
    form.add(tfUserEmail);
    form.add(tfUserPhone);

    form.add(new AjaxButton("UserSettingsSubmit", form) {

      /**
       * Serial ID.
       */
      private static final long serialVersionUID = 5809216080632369457L;

      /**
       * Handles submission of the ajax form.
       * 
       * @param target AjaxRequestTarget
       * @param form Form<?>
       */
      @Override
      protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
        info("User Information has been updated successfully.");
        target.addComponent(feedback);
      }
      
      /**
       * Handles the errors of the ajax form.
       * 
       * @param target AjaxRequestTarget
       * @param form Form<?>
       */
      @Override
      protected void onError(AjaxRequestTarget target, Form<?> form) {
        target.addComponent(feedback);
      }
    });

    add(feedback);

    add(form);
  }
}
